package com.qf.dao.impl;

import com.qf.dao.CartDao;
import com.qf.domain.CartItem;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/*
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */
public class CartDaoImpl implements CartDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public void insert(CartItem carItem) {
        String sql="insert into tb_cart(id,pid,num,money) values(?,?,?,?)";
        Object[] params={carItem.getId(),carItem.getPid(),carItem.getNum(),carItem.getMoney()};
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null, conn );
        }
    }

    @Override
    public List<CartItem> select(Integer uid) {
        String sql="select id,pid,num,money from tb_cart where id=?";
        Connection conn=DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql, new BeanListHandler<>(CartItem.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null, conn );
        }

    }

    @Override
    public CartItem select(Integer uid, Integer pid) {
        String sql="select id,pid,num,money from tb_cart where id=? and pid=?";
        Connection conn=DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql, new BeanHandler<>(CartItem.class),uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null, conn );
        }
    }

    @Override
    public void update(CartItem cartItem) {
        String sql="update tb_cart set num=?,money=? where id=? and pid=?";
        Object[] params={cartItem.getNum(),cartItem.getMoney(),cartItem.getId(),cartItem.getPid()};
        Connection conn=DataSourceUtils.getConnection();
        try {
           qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null, conn );
        }
    }

    @Override
    public void delete(Integer uid, int pid) {
        String sql="delete from tb_cart where id=? and pid=?";
        Connection conn=DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null, conn );
        }
    }

    @Override
    public void delete(Integer uid) {
        String sql="delete from tb_cart where id=?";
        Connection conn=DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null, conn );
        }
    }
}
